<!--
  * 任务配置
  *
  * @Author:    KeissCake
  * @Date:      2024-09-26 21:17:52
  * @Copyright  暂无
-->
<template>
  <a-modal
      :title="form.taskId ? '编辑' : '添加'"
      :width="Modal"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >


        <a-form-item label="目的地址"  name="transportAddress">
          <a-input style="width: 100%" v-model:value="form.transportAddress" placeholder="目的地址" />
        </a-form-item>
        <a-form-item label="对任务进行说明"  name="taskInfo">
          <a-input style="width: 100%" v-model:value="form.taskInfo" placeholder="对任务进行说明" />
        </a-form-item>
        <a-form-item label="触发类型"  name="triggerType">
          <DictSelect width="100%" v-model:value="form.triggerType" keyCode="DATATASK_TIMEWAY" placeholder="random|cycle"/>
        </a-form-item>
        <div v-if="form.triggerType[0]==='cycle'">
          <a-form-item label="周期输入" name = "triggerTime">
            <a-input style="width: 100%" v-model:value="form.triggerTime" placeholder="触发时间"/>
          </a-form-item >
        </div>
        <div v-if="form.triggerType[0]==='random'">
          <a-form-item v-show="true" label="发送时间">
            <a-date-picker
                v-model:value="releaseTime"
                :format="timeFormat"
                showTime
                :allowClear="false"
                placeholder="请选择发送时间"
                style="width: 200px"
                @change="changeTime"
            />
          </a-form-item>
        </div>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/src/components/framework/smart-loading';
  import { datataskApi } from '/src/api/business/super-computer/datatask/datatask-api';
  import { smartSentry } from '/src/lib/smart-sentry';
  import DictSelect from '/src/components/support/dict-select/index.vue';
  import dayjs from "dayjs";

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    if (form.triggerType && form.triggerType.length > 0) {
      form.triggerType = form.triggerType.map((e) => e.valueCode);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }
  // ----------------------- 执行时间 ----------------------------
  const timeFormat = 'YYYY-MM-DD HH:mm:ss';
  const releaseTime = ref(null);
  function changeTime(date, dateString) {
    form.triggerTime = dateString;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      taskId: undefined, //自增主键
      dataSourceId: undefined, //数据源表外键
      transportAddress: undefined, //目的地址
      taskInfo: undefined, //对任务进行说明
      triggerTime: undefined, //触发时间, 默认24h触发单位为秒
      triggerType: undefined, //触发类型, 默认间隔触发 conn conn式 fixed_delay 固定间隔
      enabledFlag: undefined, //禁用标识 0禁用 1开启
      updateUser: undefined, //更新人
      updateTime: undefined, //更新时间
      createUser: undefined, //创建人
      createTime: undefined, //创建时间
  };

  let form = reactive({ ...formDefault });

  const rules = {
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.taskId) {
          if (Array.isArray(form.triggerType) && form.triggerType.length > 0)
            form.triggerType = form.triggerType[0]; // 转换为字符串
        await datataskApi.update(form);
      } else {
        await datataskApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
